package cn.stu.system.controller;


import cn.stu.common.utils.R;
import cn.stu.common.utils.StringUtils;
import cn.stu.framework.entity.SysDictData;
import cn.stu.framework.service.ISysDictDataService;
import cn.stu.framework.service.ISysDictTypeService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dict/data")
@AllArgsConstructor
public class SysDictDataController {
    private ISysDictDataService dictDataService;

    private ISysDictTypeService dictTypeService;

    @GetMapping("/list")
    public R list(SysDictData dictData) {
//        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return R.ok(list);
    }


    /**
     * 查询字典数据详细
     */
    @GetMapping(value = "/{dictCode}")
    public R getInfo(@PathVariable Long dictCode) {
        return R.ok(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public R dictType(@PathVariable String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<SysDictData>();
        }
        return R.ok(data);
    }

    /**
     * 新增字典类型
     */
    @PostMapping
    public R add(@Validated @RequestBody SysDictData dict) {

        return R.ok(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */

    @PutMapping
    public R edit(@Validated @RequestBody SysDictData dict) {
        return R.ok(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */

    @DeleteMapping("/{dictCodes}")
    public R remove(@PathVariable Long[] dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return R.ok();
    }
}
